package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.RepertoryDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.RepertoryService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: zhao
 * @Date: 2020/12/4 18:59
 */
@Service
public class RepertoryServiceImpl implements RepertoryService {

    @Autowired
    private RepertoryDao repertoryDao;


    private List<DamageList> damageListList;

    private List<OverflowList> overflowLists;

    /**
     * 保存报损单
     *
     * @param damageList
     * @param damageListGoodsStr
     * @return
     */
    @Override
    public ServiceVO damageListGoods(DamageList damageList, String damageListGoodsStr, String damageNumber) {

        repertoryDao.addDamageList(damageList);
        Integer damageListId = damageList.getDamageListId();

        List<DamageListGoods> goodsList = new ArrayList<>();
        //json转换成list
        Gson gson = new Gson();
        goodsList = gson.fromJson(damageListGoodsStr, new TypeToken<List<DamageListGoods>>() {
        }.getType());

        for (DamageListGoods damageListGoods : goodsList) {
            damageListGoods.setDamageListId(damageListId);
            repertoryDao.addDamageListGoods(damageListGoods);
        }

        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**
     * 新增报溢单
     *
     * @param overflowList
     * @param overflowListGoodsStr
     * @return
     */
    @Override
    public ServiceVO overflowListGoods(OverflowList overflowList, String overflowListGoodsStr) {

        repertoryDao.addOverflowList(overflowList);
        Integer overflowListId = overflowList.getOverflowListId();

        List<OverflowListGoods> goodsList = new ArrayList<>();
        //json转换成list
        Gson gson = new Gson();
        goodsList = gson.fromJson(overflowListGoodsStr, new TypeToken<List<OverflowListGoods>>() {
        }.getType());

        for (OverflowListGoods overflowListGoods : goodsList) {
            overflowListGoods.setOverflowListId(overflowListId);
            repertoryDao.addOverflowListGoods(overflowListGoods);
        }

        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**
     * 报损单查询
     *
     * @param sTime
     * @param eTime
     * @return
     */
    @Override
    public List<DamageList> damageListGoodsList(String sTime, String eTime) {
        this.getId(sTime, eTime);
        return damageListList;
    }

    /**
     * 查询报损单商品信息
     *
     * @param damageListId
     * @return
     */
    @Override
    public List<DamageListGoods> damageListGoodsGoodsList(Integer damageListId) {

        List<DamageListGoods> damageListGoodsList = new ArrayList<>();

        for (DamageList damageList : damageListList) {
            Integer listId = damageList.getDamageListId();
            if (damageListId == listId) {
                damageListGoodsList = repertoryDao.damageListGoodsGoodsList(damageListId);
            }
        }
        return damageListGoodsList;
    }

    /**
     * 报溢单查询
     *
     * @param sTime
     * @param eTime
     * @return
     */
    @Override
    public List<OverflowList> overflowListlist(String sTime, String eTime) {
        this.getId2(sTime, eTime);
        return overflowLists;
    }

    /**
     * 报溢单商品信息
     * @param damageListId
     * @return
     */
    @Override
    public List<OverflowListGoods> overflowListGoodsgoodsList(Integer damageListId) {
        List<OverflowListGoods> overflowListGoodsList = new ArrayList<>();

        for (OverflowList overflowList : overflowLists) {
            Integer listId = overflowList.getOverflowListId();
            if (damageListId == listId) {
                overflowListGoodsList = repertoryDao.overflowListGoodsgoodsList(damageListId);
            }
        }
        return overflowListGoodsList;
    }

    /**
     * 报损单查询
     *
     * @return
     */
    public void getId(String sTime, String eTime) {
        //查询所有
        List<DamageList> damageListList = repertoryDao.getAllDamageList();

        List<DamageList> lists = new ArrayList<>();

        //2、string类型的时间转化成date类型
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        format.setLenient(false);
        try {
            Date stime = format.parse(sTime);
            Date etime = format.parse(eTime);

            for (DamageList damageList : damageListList) {
                String damageDate = damageList.getDamageDate();
                Date date = format.parse(damageDate);
                if ((date.getTime() >= stime.getTime()) && (date.getTime() <= etime.getTime())) {
                    lists.add(damageList);
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        this.damageListList = lists;
    }

    /**
     * 报损单查询
     *
     * @return
     */
    public void getId2(String sTime, String eTime) {
        //1、查询所有
        List<OverflowList> OverflowListList = repertoryDao.getAllOverflowList();

        List<OverflowList> lists = new ArrayList<>();

        //2、string类型的时间转化成date类型
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        format.setLenient(false);
        try {
            Date stime = format.parse(sTime);
            Date etime = format.parse(eTime);

            for (OverflowList overflowList : OverflowListList) {
                String damageDate = overflowList.getOverflowDate();
                Date date = format.parse(damageDate);
                if ((date.getTime() >= stime.getTime()) && (date.getTime() <= etime.getTime())) {
                    lists.add(overflowList);
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        this.overflowLists = lists;
    }


}
